package org.jeecg.config.feign;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import feign.FeignException;
import feign.Response;
import feign.Util;
import feign.codec.DecodeException;
import feign.codec.Decoder;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.lang.reflect.Type;

@Slf4j
public class FeignResultDecoder implements Decoder {

    @Override
    public Object decode(Response response, Type type) throws IOException, FeignException {
//Delete at 2023-08-01 暂时不开启这个配置
//        if (response.body() == null) {
//            throw new DecodeException(response.status(), "没有返回有效的数据", response.request());
//        }
//        String bodyStr = Util.toString(response.body().asReader(Util.UTF_8));
//
//        log.warn("FeignResultDecoder body --------------------- {}", bodyStr);
//
//        //对结果进行转换
//        TypeFactory typeFactory = objectMapper.getTypeFactory();
//        JavaType resultType = typeFactory.constructParametricType(BaseResponse.class, typeFactory.constructType(type));
//        BaseResponse<?> result = objectMapper.readValue(bodyStr, resultType);
//        //如果返回错误，且为内部错误，则直接抛出异常
//        if (!BaseConstants.HTTP_RESPONSE_CODE_SUCCESS.equals(result.getCode())) {
//            throw new DecodeException(response.status(), "接口返回错误：" + result.getMsg(), response.request());
//        }
//        return result.getData();
        return StrUtil.EMPTY;
    }
}
